// 1. 导入
const fs = require('fs')
const path = require('path')

// 封装路径
function getFullPath(fileName) {
    const fullPath = path.join(__dirname, `./data/${fileName}`)
    return fullPath
}
//! news
function news() {
    // 1. 获取绝对路径
    // const fullPath = path.join(__dirname, './data/news.json')
    const fullPath = getFullPath('news.json')
    // 2. 读文件
    const res = fs.readFileSync(fullPath, 'utf-8')
    // console.log(res)
    const news = JSON.parse(res)
    // console.log(news)
    return news
}
// news()

//! student
function students() {
    // 1. 获取绝对路径
    // const fullPath = path.join(__dirname, './data/info.json')
    const fullPath = getFullPath('info.json')
    // 2. 读文件
    const res = fs.readFileSync(fullPath, 'utf-8')
    // console.log(res)
    const info = JSON.parse(res)
    // console.log(info)
    return info
}
// students()

//! luckystar
function luckystar() {
    // 1. 获取绝对路径
    // const fullPath = path.join(__dirname, './data/info.json')
    const fullPath = getFullPath('info.json')
    // 2. 读文件
    const res = fs.readFileSync(fullPath, 'utf-8')
    // console.log(res)
    const info = JSON.parse(res)
    // console.log(info)
    // 随机数
    const i = parseInt(Math.random() * info.length)
    // console.log(i)
    const student = info[i]
    // console.log(student)
    student.count++
    // return info
    fs.writeFileSync(fullPath, JSON.stringify(info))
    return student
}
// luckystar()



module.exports = {
    news,
    students,
    luckystar
}